package org.come.Jpanel;

import com.tool.btn.ZhuShouBtn;
import org.come.model.ActiveBase;
import org.come.until.CutButtonImage;
import org.come.until.SrcollPanelUI;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ZhuShouJpanel extends JPanel {

    private JScrollPane scrollPane;
    private List<ZhuShouTaskJpanel> taskJpanelList;
    private ZhuShouBtn zhiXingBtn;

    public ZhuShouJpanel() {
        setPreferredSize(new Dimension(862, 411));
        setOpaque(false);
        setLayout(null);

        getScrollPane();
        getTaskJpanelList();
        getZhiXingBtn();
        showTask(Arrays.asList(UserMessUntil.getAllVipActive().getBases()));
    }

    public JScrollPane getScrollPane() {
        if (scrollPane == null) {
            // 属性滚动条
            scrollPane = new JScrollPane();
            scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
            scrollPane.getVerticalScrollBar().setUnitIncrement(20);
            scrollPane.getViewport().setOpaque(false);
            scrollPane.setOpaque(false);
            scrollPane.setBounds(210, 70, 490, 281);
            scrollPane.setBorder(BorderFactory.createEmptyBorder());
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            JPanel jPanel = new JPanel();
            jPanel.setBackground(null);
            jPanel.setOpaque(false);
            jPanel.setBorder(null);
            jPanel.setLayout(null);
            scrollPane.setViewportView(jPanel);
            this.add(scrollPane);

        }
        return scrollPane;
    }

    /**添加数据**/
    public void showTask(List<ActiveBase> zhuShouBeans) {
        if (zhuShouBeans == null) zhuShouBeans = new ArrayList<>();

        taskJpanelList.clear();
        for (int i = 0; i < zhuShouBeans.size(); i++) {
            ActiveBase activeBase = zhuShouBeans.get(i);
            if (activeBase.getNum() > 0) {
                ZhuShouTaskJpanel zhuShouTaskJpanel = new ZhuShouTaskJpanel(activeBase);
                taskJpanelList.add(zhuShouTaskJpanel);
            }
        }
        addData();
    }

    /** 展示滚动条里面的列表**/
    public void addData() {
        JPanel view = (JPanel) scrollPane.getViewport().getView();
        view.removeAll();
        view.repaint();

        int num = 0;
        for (int i = 0; i < taskJpanelList.size(); i++) {
            ZhuShouTaskJpanel zhuShouTaskJpanel = taskJpanelList.get(i);
            zhuShouTaskJpanel.setBounds(0, num * 58, 468, 53);
            view.add(zhuShouTaskJpanel);
            num++;
        }
        view.setPreferredSize(new Dimension(468, num * 58));
        scrollPane.updateUI();
        scrollPane.invalidate();
        scrollPane.validate();
        scrollPane.repaint();
    }

    public List<ZhuShouTaskJpanel> getTaskJpanelList() {
        if (taskJpanelList == null) {
            taskJpanelList = new ArrayList<>();
        }
        return taskJpanelList;
    }

    public ZhuShouBtn getZhiXingBtn() {
        if (zhiXingBtn == null) {
            zhiXingBtn = new ZhuShouBtn("Inkimg/button/B458.png", 1, 0, this);
            zhiXingBtn.setBounds(580, 355, 101, 30);
            this.add(this.zhiXingBtn);
        }
        return zhiXingBtn;
    }

    private ImageIcon iconBack;
    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        if (iconBack == null)
            iconBack = CutButtonImage.getImage("inkImg/background/S341.png", -1, -1);
        g.drawImage(iconBack.getImage(), 0, 0, 862, 411, this);
    }

    public List<ActiveBase> getAllActive() {
        List<ActiveBase> zhuShouBeans = new ArrayList<>();
        for (int i = 0; i < taskJpanelList.size(); i++) {
            ZhuShouTaskJpanel zhuShouTaskJpanel = taskJpanelList.get(i);
            if (zhuShouTaskJpanel.isSlelct()) zhuShouBeans.add(zhuShouTaskJpanel.getZhuShouBean());
        }
        return zhuShouBeans;
    }
}
